package pa165.pneu.pneuservisbackend.services;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pa165.pneu.pneuservisbackend.dao.CustomerDAO;
import pa165.pneu.pneuservisbackend.entiies.Customer;
import pa165.pneu.pneuservisbackend.to.CustomerTO;
/**
 * 
 * @author Juraj Palenik
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDAO customerDAO;

    @Transactional
    @Override
    public void createCustomer(CustomerTO customer) throws IllegalArgumentException {
        customerDAO.createCustomer(customer.toCustomer());
    }

    @Transactional
    @Override
    public CustomerTO getCustomer(Long id) throws IllegalArgumentException {
        return new CustomerTO(customerDAO.getCustomer(id));
    }

    @Transactional
    @Override
    public void updateCustomer(CustomerTO customer) throws IllegalArgumentException {
        customerDAO.updateCustomer(customer.toCustomer());
    }

    @Transactional
    @Override
    public void removeCustomer(CustomerTO customer) throws IllegalArgumentException {
        customerDAO.removeCustomer(customer.toCustomer());
    }

    @Transactional
    @Override
    public List<CustomerTO> getAllCustomers() {
        List<CustomerTO> list = new ArrayList();
        for (Customer s : customerDAO.getAllCustomers()) {
            list.add(new CustomerTO(s));
        }
        return list;
    }

    @Transactional
    @Override
    public List<CustomerTO> findCustomerByName(String name) throws IllegalArgumentException {
        List<CustomerTO> list = new ArrayList();
        for (Customer s : customerDAO.findCustomerByName(name)) {
            list.add(new CustomerTO(s));
        }
        return list;
    }
}